package com.ld.vaadin.view;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.component.login.LoginI18n;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.auth.AnonymousAllowed;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

@Slf4j
@PageTitle("登录页面")
@Route(value = "login")
@AnonymousAllowed
public class IndexView extends VerticalLayout {

    public IndexView(AuthenticationManager authManager) {

        setSizeFull();
//        水平居中
        setAlignItems(Alignment.CENTER);
//        没有作用
//        setHorizontalComponentAlignment(Alignment.CENTER);
//        垂直居中
        setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);

        LoginForm loginForm = getLoginForm();
//        点击按钮后使用post请求该路径
//        loginForm.setAction("/api/form/v1/user/authenticate");
//        loginForm.setAction("#");
        // 处理登录事件
        loginForm.addLoginListener(e -> {
            String username = e.getUsername();
            String password = e.getPassword();

            try {
                // 调用Spring Security认证
                Authentication authentication = authManager.authenticate(
                        new UsernamePasswordAuthenticationToken(username, password)
                );
                SecurityContextHolder.getContext().setAuthentication(authentication);

                // 登录成功后跳转到首页
//                UI.getCurrent().navigate("/page/home");
                UI.getCurrent().getPage().setLocation("/page/home");
            } catch (Exception ex) {
                // 登录失败时显示错误信息
                loginForm.setError(true);
            }
        });
        loginForm.addForgotPasswordListener(forgotPasswordEvent -> getUI().ifPresent(ui -> ui.navigate("/register")));
        add(loginForm);

//        var a = new Anchor("/register", "注册");
//        add(a);

    }

    private LoginForm getLoginForm() {

        LoginForm loginForm = new LoginForm();

        LoginI18n loginI18n = new LoginI18n();

        LoginI18n.Form form = new LoginI18n.Form();
        form.setTitle("登录");
        form.setUsername("请输入账号");
        form.setPassword("请输入密码");
        form.setSubmit("登录");
        form.setForgotPassword("没有账号？点击注册");
        loginI18n.setForm(form);

        LoginI18n.ErrorMessage errorMessage = new LoginI18n.ErrorMessage();
        errorMessage.setUsername("账号不能为空");
        errorMessage.setPassword("密码不能为空");
        loginI18n.setErrorMessage(errorMessage);

        loginForm.setI18n(loginI18n);
        return loginForm;
    }

//    private void login(AbstractLogin.LoginEvent loginEvent) {
//        String username = loginEvent.getUsername();
//        String password = loginEvent.getPassword();
//
//        var request = AuthenticationRequest.builder()
//                .email(username)
//                .credentials(password)
//                .build();
//        AuthenticationResponse response = authenticationService.authenticate(request);
//        String token = response.getToken();
////        Storage 库
//        WebStorage.setItem("Authorization", "bearer " + token);
//
//
//    }

}
